﻿using System.Linq;
using System.Web.Mvc;
using ExChange.Model;
using System.Data.Entity;

namespace ExChange.Web.Controllers
{
    public class WareController : BaseController
    {
        public JFDHEntities entity = new JFDHEntities();
        public ActionResult Index()
        {
            return View();
        }

        //生成二维码方法一
        public ActionResult Create2Code(string nr)
        {
            ThoughtWorks.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new ThoughtWorks.QRCode.Codec.QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 10;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeErrorCorrect = ThoughtWorks.QRCode.Codec.QRCodeEncoder.ERROR_CORRECTION.M;
            System.Drawing.Image image = qrCodeEncoder.Encode(nr);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ContentType="image/png";
            var byts=stream.GetBuffer();
            Response.OutputStream.Write(byts, 0, byts.Length);
            return View();
        }



        public ActionResult GetList(int page, int rows, string Name = "")
        {
            //var list = entity.Ware.ToList();
            var list = from o in entity.Ware select o;
            if (!string.IsNullOrEmpty(Name))
            {
                list = list.Where(f => f.Name.Contains(Name));
            }
            var totalcount = list.Count();
            var data = list.OrderByDescending(f => f.ID).Skip((page - 1) * rows).Take(rows).ToList().Select(f => new
            {
                f.Count,
                f.ID,
                f.Pirce,
                f.Remark,
                f.ImageUrl,
                f.Name,
                f.ConvertedQuantity,
            });
            return Json(new { rows = data, total = totalcount }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Save(Ware model)
        {
            //string pathList = Request["pathList"]; 
            if (model.ID == 0)
            {
                model.ConvertedQuantity = 0;
                entity.Ware.Add(model);
            }
            else
            {
                entity.Entry<Ware>(model).State = EntityState.Modified;
            }
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }

        public ActionResult Del(int id)
        {
            var model = entity.Ware.FirstOrDefault(f => f.ID == id);
            entity.Ware.Remove(model);
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }
    }
}
